import urllib.request
import urllib.parse

# https://tieba.baidu.com/f?ie=utf-8&kw=%E4%BC%A0%E6%99%BA%E4%B8%93%E4%BF%AE%E5%AD%A6%E9%99%A2


def loadPage(url, filename):
    """
    作用:根据url发送请求,获取服务器响应文件
    需要爬取得url地址
    filename:写入文件名
    """
    print('正在下载'+filename)
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3969.0 Safari/537.36"
    }
    req=urllib.request.Request(url,headers=headers)
    return urllib.request.urlopen(req).read().decode("utf8")



def writePage(html,filename):
    """
    作用:将html内容写入到本地
    html:服务器响应文件内容
    """
    print("正在保存"+filename)
    with open(filename, 'w',encoding='utf-8') as f:
        f.write(html)

def tiebaSpider(url, beginPage, endPage):
    """
    作用:贴吧爬虫调度器,负责组合每个页面的url
    url:贴吧url的前部分
    beginPage:起始页
    endPage:结束页
    """
    for page in range(beginPage, endPage+1):
        pn = (page-1)*50
        fullurl = url+"&pn="+str(pn)
        filename = "第"+str(page)+"页.html"
        html=loadPage(fullurl, filename)
        writePage(html,filename)

if __name__ == '__main__':
    kw = input("请输入要爬取的贴吧名字:")
    beginPage = int(input("请输入起始页:"))
    endPage = int(input('请输入结束页:'))

    url = ' http://tieba.baidu.com/f?'
    key = urllib.parse.urlencode({"kw": kw})  # 解码
    fullurl = url+key
    tiebaSpider(fullurl, beginPage, endPage)
